﻿var improtNum = 0;

/**
* @description 关闭当前页 返回父页面
*/
function closePage(isReload) {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭
    isReload = improtNum > 0 ? true : false; //导出成功返回刷新列表
    if (isReload) {
        parent.location.reload();
    }
}

layui.use(['layer', 'form', 'laydate', 'upload'], function () {
    /**
    * @description 加载layer部件
    */
    var layer = layui.layer,
        form = layui.form,
        laydate = layui.laydate,
        upload = layui.upload;

    //批量导入
    var url = '/Business/Common/UploadifyHandler.ashx?ram=' + Math.random() + '&PostType=uploadPerson';

    //隐藏上传按钮
    //后台上传读取暂时只支持xls
    var uploadInst = upload.render({
        elem: '#tbUploadFile'
        , url: url
        , accept: 'file'
        , exts: 'xls|xlsx'
        , acceptMime: 'file/xls,file/xlsx'
        , done: function (data) {
            if (data.success) {
                var dataObj = data.obj;
                var ahtml = '<a href="' + dataObj.uploadUrl + '" target="_blank">' + dataObj.fileName + '</a>';
                $('#tbUploadUrl').attr("data-url", dataObj.uploadUrl);
                $('#tbUploadUrl').html(ahtml);
                if (dataObj.trueNum > 0) {
                    $('#successMsg').html('正确数据' + dataObj.trueNum + '条');
                }
                if (dataObj.falseNum > 0) {
                    $('#falseMsg').html('错误数据' + dataObj.falseNum + '条 <a href="' + dataObj.errorFileUrl + '" target="_blank">点击下载错误信息</a>');
                }
            } else {
                layer.msg(data.msg);
            }
        }
    });

    //重载上传按钮
    window.uploadFile = function () {
        //重载上传路径参数 这里用不上
        //var projectGUID = $('#tbProjectGUID').val();
        //if (!projectGUID) {
        //    layer.msg('请选择所属项目');
        //    return;
        //}
        //var payMonth = $('#tbPayMonth').val();
        //if (!payMonth) {
        //    layer.msg('请选择工资年月');
        //    return;
        //}

        //uploadInst.reload({
        //    url: url + "&ProjectGUID=" + projectGUID + "&PayMonth=" + payMonth
        //});
        $('#tbUploadFile').click();
    }

    $(function () {

    });

    /**
    * @description 保存
    */
    form.on('submit(save)', function (data) {
        var uploadUrl = $('#tbUploadUrl').attr("data-url");
        if (!uploadUrl) {
            layer.msg("请上传文件");
            return false;
        }

        var fieldData = data.field;
        fieldData["UploadUrl"] = uploadUrl;
        fieldData["PostType"] = "savePerson";
        app.ajax("/Business/Common/UploadifyHandler.ashx", fieldData, function (data) {
            layer.msg(data.msg);
            if (data.success) {
                $('#tbUploadUrl').attr("data-url", "");
                $('#tbUploadUrl').html('');

                improtNum = data.rows.length;
                var trhtmls = "";
                for (var i = 0; i < data.rows.length; i++) {
                    trhtmls += "<tr>"
                        + "<td>" + data.rows[i].RowNum + "</td>"
                        + "<td>" + data.rows[i].PersonName + "</td>"
                        + "<td>" + data.rows[i].IDCard + "</td>"
                        + "<td>" + data.rows[i].PersonSex + "</td>"
                        + "<td>" + data.rows[i].PersonNation + "</td>"
                        + "<td>" + data.rows[i].Address + "</td>"
                        + "<td>" + data.rows[i].Organize + "</td>"
                        + "<td>" + data.rows[i].BeginDate + "至" + data.rows[i].EndDate + "</td>"
                        + "<td>" + data.rows[i].LinkPhone + "</td>"
                        + "</tr>";
                }
                $("#tBodyDetail").html(trhtmls);
            }
        }, function (err) {
            layer.msg('保存失败');
        });
        return false;
    });
});